package com.googlecode.continuity.sample.basic.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.InvocationException;
import com.google.gwt.user.client.ui.*;
import com.googlecode.continuity.core.client.CoreGWT;
import com.googlecode.continuity.plugin.codecomment.client.CodeCommentScreen;
import com.googlecode.continuity.plugin.codecomment.client.CodePointer;
import com.googlecode.continuity.plugin.gwtext.client.ErrorBox;
import com.googlecode.continuity.plugin.gwtext.client.LoaderBox;
import com.googlecode.continuity.plugin.svnbrowser.client.*;
import com.googlecode.continuity.plugin.svnbrowser.client.wrapper.SVNDirEntryWrapper;

/**
 * TestPage
 *
 * @author Peter Schmitt <aragos@aragos.de>
 */
public class TestPage implements EntryPoint {

  public void onModuleLoad() {

    FlexTable table = new FlexTable();
    table.setWidget(0, 0,
        new Label("Enter repository location and credentials (blank credentials will result in anonymous login):"));
    table.getFlexCellFormatter().setColSpan(0, 0, 2);
    table.setWidget(1, 0, new Label("Repository Location:"));
    final TextBox repositoryLocation = new TextBox();
    table.setWidget(1, 1, repositoryLocation);
    repositoryLocation.setWidth("300px");

    table.setWidget(2, 0, new Label("Username:"));
    final TextBox username = new TextBox();
    table.setWidget(2, 1, username);

    table.setWidget(3, 0, new Label("Password:"));
    final TextBox password = new TextBox();
    table.setWidget(3, 1, password);

    Button browse = new Button("Browse", new ClickListener() {
      public void onClick(Widget widget) {
        final String repositoryURL = repositoryLocation.getText();
        final ReadSVNRPCAsync svnRPCAsync = (ReadSVNRPCAsync) CoreGWT.getRPC("ReadSVNRPCAsync");

        // retrieve repository connection
        svnRPCAsync.connect(repositoryURL, username.getText(), password.getText(), new AsyncCallback() {
          public void onFailure(Throwable throwable) {
            ErrorBox box = new ErrorBox(throwable);
            box.show();
          }

          // show file browser
          public void onSuccess(Object object) {
            final SVNRepositoryHandle handle = (SVNRepositoryHandle) object;
            SVNBrowser svnBrowser = new SVNBrowser(handle);
            svnBrowser.setRootPrefix("Project 15");
            svnBrowser.show();

            // listen for file browser selections
            svnBrowser.addSVNBrowserEventListener(new SVNBrowserEventListener() {
              public void onDirectorySelected(String directoryPath) {
              }

              // retrieve file contents
              public void onFileSelected(SVNDirEntryWrapper entry, String filePath) {
                final CodePointer pointer = new CodePointer();
                pointer.setUrl(repositoryURL + filePath);
                pointer.setName(entry.getName());
                pointer.setFirstLine(1);
                pointer.setVersion(entry.getRevision());

                //todo: show loader
                final PopupPanel loader = new LoaderBox("Loading file contents...");
                loader.show();

                // show file contents
                svnRPCAsync.getTextFileContents(handle,
                    pointer.getUrl().substring(handle.getRepositoryLocation().length()), pointer.getVersion(),
                    new AsyncCallback() {
                      public void onFailure(Throwable throwable) {
                        if (!loader.isVisible()) {
                          return;
                        }
                        loader.hide();
                        String error = "";
                        try {
                          throw throwable;
                        } catch (InvocationException e) {
                          error = e.getMessage();
                        } catch (ReadSVNException e) {
                          error = e.getMessage();
                        } catch (Throwable t) {
                          GWT.getUncaughtExceptionHandler().onUncaughtException(t);
                        }
                        ErrorBox box = new ErrorBox(error);
                        box.show();
                      }

                      public void onSuccess(Object object) {
                        if (!loader.isVisible()) {
                          return;
                        }
                        String code = (String) object;
                        CodeCommentScreen box = new CodeCommentScreen(handle, pointer, code);
                        loader.hide();
                        box.show();
                      }
                    });
              }
            });
          }
        });
      }
    });
    table.setWidget(4, 0, browse);
    table.getFlexCellFormatter().setColSpan(4, 0, 2);
    table.getFlexCellFormatter().setHorizontalAlignment(4, 0, HasHorizontalAlignment.ALIGN_RIGHT);
/*
    DOM.setStyleAttribute(browse.getElement(), "padding", "0 2px");
    DOM.setStyleAttribute(browse.getElement(), "border", "1px solid #999");
    DOM.setStyleAttribute(browse.getElement(), "background", "#ddd");
    DOM.setStyleAttribute(browse.getElement(), "color", "#555");*/

    table.setWidget(5, 0, new Label("(No repository in mind? Try http://continuity.googlecode.com/svn/trunk/)"));
    table.getFlexCellFormatter().setColSpan(5, 0, 2);
    table.getFlexCellFormatter().setHorizontalAlignment(5, 0, HasHorizontalAlignment.ALIGN_RIGHT);

    RootPanel.get("col3_content").add(table);
  }
}
